package com.example.webflux.webflux.demo2.handler;

import com.alibaba.fastjson.JSON;
import com.example.webflux.webflux.pojo.Student;
import com.example.webflux.webflux.service.StudentService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.stream.Collectors;

/**
 *@Deacription TODO
 *@Author baohuajie
 *@Date 2020/12/30 20:56
 *@Version 1.0
 **/
@Component
public class MyHandler {
    @Autowired
    private StudentService studentService;

    public Mono<ServerResponse> getUserById(ServerRequest request) {
        Flux<Student> studentFlux = studentService.queryAll();
        Mono<ServerResponse> body = ServerResponse.ok().body(studentFlux, Student.class);
        return body;
    }

    public Mono<ServerResponse> getUserByName(ServerRequest request) {

        Flux<Student> reqData = request.bodyToFlux(Student.class);
        System.out.println(Thread.currentThread().getName());
        reqData.subscribe(c -> {
            String s = JSON.toJSONString(c);
            System.out.println(s);
            System.out.println(Thread.currentThread().getName());
        });
        Flux<Student> studentFlux = studentService.queryAll();
        Mono<ServerResponse> body = ServerResponse.ok().body(studentFlux, Student.class);
        return body;

    }

}
